package com.linln.admin.app.service.task;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.shared.invoker.InvocationOutputHandler;

import java.io.IOException;

/**
 * 存放一些自定义的内容
 */
@Slf4j
public class MavenInvokerBean {
    public static class EmptyInvocationOutputHandler implements InvocationOutputHandler {
        private String err = null;

        @Override
        public void consumeLine(String s) throws IOException {
            log.info(s);
            if (StringUtils.isBlank(err) && s.startsWith("[ERROR] Failed to execute")) {
                err = s;
            }
        }

        public String getErrorMessage() {
            if (err.contains("does not allow updating")) {
                String projectName = "";
                int idx = err.indexOf("on project ");
                if (idx > 0) {
                    projectName = err.substring(idx + "on project ".length(), err.indexOf(":", idx));
                }
                return projectName + "该版本的包已经在仓库中存在！";
            } else if (err.contains("status: 401 Unauthorized")) {
                return "没有权限上传包，请配置正确的账号名！";
            } else if (err.contains("Could not resolve dependencies for project ")) {
                String tmp = err.substring(err.indexOf("Could not resolve dependencies for project ") + "Could not resolve dependencies for project ".length());
                if (tmp.contains("Could not find artifact ")) {
                    tmp = tmp.substring(tmp.indexOf("Could not find artifact ") + "Could not find artifact ".length());
                } else if (tmp.contains("Failure to find ")) {
                    tmp = tmp.substring(tmp.indexOf("Failure to find ") + "Failure to find ".length());
                }
                if (tmp.contains(" ")) {
                    tmp = tmp.substring(0, tmp.indexOf(" "));
                }
                return "请先打包上传" + tmp + "包！";
            }
            return err;
        }
    }
}
